package com.qf.microuser.user.controller;

import cn.hutool.core.util.RandomUtil;
import com.qf.common.core.annotation.NoLogin;
import com.qf.common.core.common.R;
import com.qf.common.core.entity.User;
import com.qf.common.core.utils.UserUtils;
import com.qf.common.minio.utils.MinioUtils;
import com.qf.microuser.user.mapper.UserMapper;
import com.qf.microuser.user.pojo.dto.LoginParam;
import com.qf.microuser.user.pojo.dto.UserDTO;
import com.qf.microuser.user.pojo.vo.MyVO;
import com.qf.microuser.user.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

/**
 * --- 天道酬勤 ---
 *
 * @author QiuShiju
 * @date 2025/11/4
 * @desc
 */
@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private IUserService userService;

    @Autowired
    private MinioUtils minioUtils;

    @Autowired
    private UserMapper userMapper;

    /**
     * 注册
     */
    @PostMapping("/register")
    @NoLogin // 不需要认证即可访问
    public R register(@RequestBody @Validated UserDTO userDTO) {
        userService.register(userDTO);
        return R.ok();
    }

    /**
     * 发送手机验证码
     */
    @GetMapping("/getCode")
    @NoLogin // 不需要认证即可访问
    public R getCode(String phone) {
        String code = userService.sendCode(phone);
        return R.ok(code);
    }


    /**
     * 登录,成功后返回jwt的token
     */
    @PostMapping("/login")
    @NoLogin // 不需要认证即可访问
    public R login(@RequestBody @Validated LoginParam loginParam){
        String token = userService.login(loginParam);
        return R.ok(token);
    }

    /**
     * 测试接口,验证拦截是否生效
     */
    @GetMapping("/test")
    public R test(){
        return R.ok("验证登录拦截的test执行");
    }

    /**
     * 查询用户信息
     */
    @GetMapping("/my")
    public R my(){

        MyVO myVO = userService.getInfo();
        return R.ok(myVO);
    }

    /**
     * 上传头像
     */
    @PostMapping("/upload/head")
    public R uploadHead(@RequestParam("file") MultipartFile file) throws IOException {
        String fileName = RandomUtil.randomString(10);
        String[] split = file.getOriginalFilename().split("\\.");
        String suffix = split[split.length-1];

        String avatar = minioUtils.upload(fileName+"."+suffix, file.getInputStream());

        User user = new User();
        Integer uid = UserUtils.getUid();
        user.setId(uid);
        user.setAvatar(avatar);
        userService.updateById(user);
        return R.ok(avatar);
    }
    /**
     * 获取用户信息
     */
    @GetMapping("/info")
    public R info(){
        Integer uId = UserUtils.getUid();
        User user =  userMapper.selectById(uId);
        return R.ok(user);
    }
    /**
     * 修改用户信息
     */
    @PostMapping("/update")
    public R update(@RequestBody User user){
        Integer uid = UserUtils.getUid();
        user.setId(uid);
        userService.updateById(user);
        return R.ok();
    }

}
